﻿using Basic.FlowCollect;
using Basic.FlowLocalEvent.Model;
using Basic.FlowModel.Node;
using WeDonekRpc.Client.Attr;
using WeDonekRpc.Client.Interface;
using WeDonekRpc.Helper;

namespace Basic.FlowLocalEvent.Route
{
    [LocalEventName("Delete")]
    internal class RefreshNode : IEventHandler<FlowNodeRouteEvent>
    {
        private readonly IFlowNodeCollect _Node;
        private readonly IFlowCollect _Flow;
        public RefreshNode ( IFlowNodeCollect node, IFlowCollect flow )
        {
            this._Node = node;
            this._Flow = flow;
        }

        public void HandleEvent ( FlowNodeRouteEvent data, string eventName )
        {
            ToNoteBase node = this._Node.Get<ToNoteBase>(data.Route.ToNodeId);
            if ( node.IntoFormula.IsNull() == false )
            {
                long nodeId = data.Route.NodeId;
                int index = node.IntoFormula.FindIndex(c => c.NodeId == nodeId);
                if ( index != -1 )
                {
                    node.IntoFormula = node.IntoFormula.Remove(index);
                    this._Node.SetIntoFormula(node.Id, node.IntoFormula);
                }
            }
        }
    }
}